<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":true},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"./public/search.xml"};
  </script>

  <meta name="description" content="字面量：数字和字符串“字面量”即常量，是固定值，不可改变。看见什么，它就是什么。 字面量有3种：  数字  字符串  布尔字面量">
<meta property="og:type" content="article">
<meta property="og:title" content="02-变量">
<meta property="og:url" content="http://yoursite.com/2020/07/10/02-%E5%8F%98%E9%87%8F/index.html">
<meta property="og:site_name" content="GipBaby270&#39;s Blog">
<meta property="og:description" content="字面量：数字和字符串“字面量”即常量，是固定值，不可改变。看见什么，它就是什么。 字面量有3种：  数字  字符串  布尔字面量">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://img.smyhvae.com/20180116_2020.png">
<meta property="og:image" content="http://img.smyhvae.com/20180116_2040.png">
<meta property="article:published_time" content="2020-07-10T05:23:04.458Z">
<meta property="article:modified_time" content="2020-07-10T05:31:53.454Z">
<meta property="article:author" content="Gip886">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://img.smyhvae.com/20180116_2020.png">

<link rel="canonical" href="http://yoursite.com/2020/07/10/02-%E5%8F%98%E9%87%8F/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>02-变量 | GipBaby270's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    
    <a href="https://github.com/Gip886" target="_blank" rel="noopener" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#64CEAA; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
    
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">GipBaby270's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Fighting for BAT!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/07/10/02-%E5%8F%98%E9%87%8F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/%E5%A4%B4%E5%83%8F.jpg">
      <meta itemprop="name" content="Gip886">
      <meta itemprop="description" content="为了更美好的明天而战">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="GipBaby270's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          02-变量
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-07-10 13:23:04 / 修改时间：13:31:53" itemprop="dateCreated datePublished" datetime="2020-07-10T13:23:04+08:00">2020-07-10</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/JS%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">JS基础</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>2.7k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>2 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="字面量：数字和字符串"><a href="#字面量：数字和字符串" class="headerlink" title="字面量：数字和字符串"></a>字面量：数字和字符串</h2><p>“字面量”即<strong>常量</strong>，是固定值，不可改变。看见什么，它就是什么。</p>
<p>字面量有3种：</p>
<ul>
<li><p>数字</p>
</li>
<li><p>字符串</p>
</li>
<li><p>布尔字面量</p>
</li>
</ul>
<a id="more"></a>

<p>（1）数值的字面量非常简单，写上去就行，不需要任何的符号。例如：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">alert(<span class="number">886</span>);  <span class="comment">//886是数字，所以不需要加引号。</span></span><br></pre></td></tr></table></figure>

<p>（2）字符串的字面量也很简单，但一定要加上引号。可以是单词、句子等。例如：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">'886'</span>);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'千古壹号，永不止步'</span>);</span><br></pre></td></tr></table></figure>

<p>温馨提示：100是数字，”100”是字符串。</p>
<p>（3）布尔字面量举例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">'如果为真，就走这里面的代码);</span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></figure>

<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>字面量都可以直接使用，但是我们一般不会直接使用字面量。</p>
<p>如果直接使用字面量的话，比较麻烦。比如说，多个地方要用到同一个字面量，还不如事先定义一个变量，用来保存字面量。</p>
<p>变量更方便我们使用，所以在开发中都是通过变量去保存一个字面量，而不会直接使用字面量。</p>
<h2 id="变量的概念"><a href="#变量的概念" class="headerlink" title="变量的概念"></a>变量的概念</h2><p><strong>变量</strong>：是用于存放数据的容器。我们通过「变量名」获取数据，甚至可以修改数据。</p>
<p>变量还可以用来保存字面量。</p>
<p><strong>本质</strong>：变量是程序在内存中申请的一块用来存放数据的空间。</p>
<h2 id="变量的声明和赋值"><a href="#变量的声明和赋值" class="headerlink" title="变量的声明和赋值"></a>变量的声明和赋值</h2><h3 id="变量的声明-变量的定义"><a href="#变量的声明-变量的定义" class="headerlink" title="变量的声明/变量的定义"></a>变量的声明/变量的定义</h3><p>在ES6语法之前，统一使用<code>var</code>关键字来声明一个变量。比如：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> name; <span class="comment">// 声明一个名为 name 的变量</span></span><br></pre></td></tr></table></figure>


<p>PS：<strong>在JavaScript中，永远都是用var来定义变量</strong>（在ES6 之前），这和C、Java等语言不同。</p>
<h3 id="变量的声明-定义（ES6）"><a href="#变量的声明-定义（ES6）" class="headerlink" title="变量的声明/定义（ES6）"></a>变量的声明/定义（ES6）</h3><p>在ES6语法及之后，可以使用 <code>const</code>、<code>let</code>关键字来声明一个变量</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name; <span class="comment">// 定义一个常量</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> age;</span><br></pre></td></tr></table></figure>

<p>如果你想定义一个常量，就用 const；如果你想定义一个变量，就用 let。</p>
<h3 id="变量的赋值"><a href="#变量的赋值" class="headerlink" title="变量的赋值"></a>变量的赋值</h3><p>变量的赋值：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">name= <span class="string">'千古壹号'</span></span><br></pre></td></tr></table></figure>

<p>变量的声明和赋值，写在一起，举例如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">100</span>; <span class="comment">// ES5语法</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> b = hello; <span class="comment">// ES6 语法</span></span><br><span class="line"><span class="keyword">let</span> b = world; <span class="comment">// ES6 语法</span></span><br></pre></td></tr></table></figure>

<p>如下图所示：</p>
<p><img src="http://img.smyhvae.com/20180116_2020.png" alt></p>
<p>var是英语“variant”变量的缩写。后面要加一个空格，空格后面的东西就是“变量名”：</p>
<ul>
<li><p>定义变量：var就是一个<strong>关键字</strong>，用来定义变量。所谓关键字，就是有特殊功能的单词。</p>
</li>
<li><p>变量的赋值：等号表示<strong>赋值</strong>，将等号右边的值，赋给左边的变量。</p>
</li>
<li><p>变量名：我们可以给变量任意的取名字。</p>
</li>
</ul>
<h3 id="变量的初始化"><a href="#变量的初始化" class="headerlink" title="变量的初始化"></a>变量的初始化</h3><p>有经验的程序员，会把声明和赋值写在一起：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">100</span>;    <span class="comment">//声明，并且赋值100</span></span><br><span class="line"><span class="built_in">console</span>.log(a);  <span class="comment">//输出100</span></span><br></pre></td></tr></table></figure>

<p>声明一个变量并赋值， 我们称之为<strong>变量的初始化</strong>。</p>
<h2 id="变量声明和赋值的补充"><a href="#变量声明和赋值的补充" class="headerlink" title="变量声明和赋值的补充"></a>变量声明和赋值的补充</h2><h3 id="修改变量的值"><a href="#修改变量的值" class="headerlink" title="修改变量的值"></a>修改变量的值</h3><p>一个变量被重新复赋值后，它原有的值就会被覆盖，变量值将以最后一次赋的值为准。</p>
<p>举例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">100</span>;</span><br><span class="line">a = <span class="number">110</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">// 打印结果：110。因为 110 覆盖了 100</span></span><br></pre></td></tr></table></figure>


<h3 id="同时声明多个变量"><a href="#同时声明多个变量" class="headerlink" title="同时声明多个变量"></a>同时声明多个变量</h3><p>同时声明多个变量时，只需要写一个 var， 多个变量名之间用英文逗号隔开。</p>
<p>举例：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> name = <span class="string">'千古壹号'</span>, age = <span class="number">27</span>, number = <span class="number">100</span>;</span><br></pre></td></tr></table></figure>

<h3 id="变量声明的几种特殊情况"><a href="#变量声明的几种特殊情况" class="headerlink" title="变量声明的几种特殊情况"></a>变量声明的几种特殊情况</h3><p>变量建议先声明，再使用。否则可能会报错。具体如下。</p>
<p><strong>写法1</strong>、先声明，再赋值：（正常）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a;</span><br><span class="line">a = <span class="number">100</span>;</span><br><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">// 打印结果：100</span></span><br></pre></td></tr></table></figure>
<p><strong>写法2</strong>、不声明，只赋值：（正常）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="number">100</span>;</span><br><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">// 打印结果：100</span></span><br></pre></td></tr></table></figure>

<p><strong>写法3</strong>、只声明，不赋值：（注意，打印 undefined）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a;</span><br><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">// 打印结果：undefined</span></span><br></pre></td></tr></table></figure>

<p><strong>写法4</strong>、不声明，不赋值，直接使用：（会报错）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(a); <span class="comment">// 会报错</span></span><br></pre></td></tr></table></figure>

<p>控制台会报错：</p>
<p><img src="http://img.smyhvae.com/20180116_2040.png" alt></p>
<p><strong>补充</strong>：写法1和写法2虽然都正常，但这两种写法是有区别的，等以后学习了「变量提升」的概念就明白了。大多数情况下，我们都是采用的写法1。</p>
<h2 id="变量的命名规范"><a href="#变量的命名规范" class="headerlink" title="变量的命名规范"></a>变量的命名规范</h2><p>大写字母是可以使用的，并且大小写敏感。也就是说A和a是两个变量。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> A = <span class="number">250</span>;    <span class="comment">//变量1</span></span><br><span class="line"><span class="keyword">var</span> a = <span class="number">888</span>;    <span class="comment">//变量2</span></span><br></pre></td></tr></table></figure>

<p>我们来整理一下<strong>变量的命名规范</strong>：</p>
<ul>
<li><p>只能由字母(A-Z、a-z)、数字(0-9)、下划线(_)、美元符( $ )组成</p>
</li>
<li><p>不能以数字开头。也就是说，必须以字母(A-Z、a-z)、下划线(_)或者美元符( $ )开头。变量名中不允许出现空格。</p>
</li>
</ul>
<ul>
<li>不用使用 JS 语言中保留的「关键字」和「保留字」作为变量名。</li>
</ul>
<ul>
<li><p>建议用驼峰命名规则。比如getElementById、matherAndFather、aaaOrBbbAndCcc</p>
</li>
<li><p>变量名会区分大小写(javascript 是区分大小写的语言)。</p>
</li>
<li><p>变量名长度不能超过255个字符。</p>
</li>
<li><p>汉语可以作为变量名。但是不建议使用，因为 low。</p>
</li>
</ul>
<h2 id="标识符、关键字、保留字"><a href="#标识符、关键字、保留字" class="headerlink" title="标识符、关键字、保留字"></a>标识符、关键字、保留字</h2><h3 id="标识符"><a href="#标识符" class="headerlink" title="标识符"></a>标识符</h3><p><strong>标识符</strong>：在JS中所有的可以由我们<strong>自主命名</strong>的都可以称之为标识符。</p>
<p>例如：变量名、函数名、属性名、参数名都是属于标识符。通俗来讲，标识符就是我们写代码时为它们起的名字。</p>
<p><strong>标识符的命名规则</strong>和变量的命令规则是一样的。看上面一段就可以了。</p>
<p>同样，标识符不能使用语言中保留的<strong>关键字</strong>及<strong>保留字</strong>。如下。</p>
<h3 id="关键字"><a href="#关键字" class="headerlink" title="关键字"></a>关键字</h3><p><strong>关键字</strong>：是指 JS 本身已经使用了的单词，我们不能再用它们充当变量、函数名等标识符。</p>
<p>JS 中的关键字如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">break、continue、case、default、</span><br><span class="line"></span><br><span class="line">if、else、switch、for、in、do、while、</span><br><span class="line"></span><br><span class="line">try、catch、finally、throw、</span><br><span class="line"></span><br><span class="line">var、void、function、return、new、</span><br><span class="line"></span><br><span class="line">this、typeof、instanceof、delete、with、</span><br><span class="line"></span><br><span class="line">true、false、null、undefined</span><br></pre></td></tr></table></figure>

<h3 id="保留字"><a href="#保留字" class="headerlink" title="保留字"></a>保留字</h3><p><strong>保留字</strong>：实际上就是预留的“关键字”。意思是现在虽然还不是关键字，但是未来可能会成为关键字，同样不<br>能使用它们当充当变量名、函数名等标识符。</p>
<p>JS 中的保留字如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">abstract、boolean、byte、char、class、const、</span><br><span class="line"></span><br><span class="line">debugger、double、enum、export、extends、final、float、goto</span><br><span class="line"></span><br><span class="line">implements、import、int、interface、long、native、package、</span><br><span class="line"></span><br><span class="line">private、protected、public、short、static、super、synchronized、throws、</span><br><span class="line"></span><br><span class="line">transient、volatile</span><br></pre></td></tr></table></figure>



<h2 id="举例"><a href="#举例" class="headerlink" title="举例"></a>举例</h2><p><strong>举例1</strong>：交换两个变量的值</p>
<p>代码实现：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a1 = <span class="number">100</span>;</span><br><span class="line"><span class="keyword">var</span> a2 = <span class="number">200</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> temp;</span><br><span class="line"></span><br><span class="line">temp = a1;</span><br><span class="line">a1 = a2;</span><br><span class="line">a2 = temp;</span><br></pre></td></tr></table></figure>



    </div>

    
    
    
        <div class="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续分享！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.png" alt="Gip886 微信支付">
        <p>微信支付</p>
      </div>

  </div>
</div>


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/07/09/2020.7.9%E6%9F%A5%E7%9C%8BHexo%E5%AE%98%E6%96%B9%E6%96%87%E6%A1%A3/" rel="prev" title="2020.7.9查看Hexo官方文档">
      <i class="fa fa-chevron-left"></i> 2020.7.9查看Hexo官方文档
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/07/11/%E5%A4%A7%E5%AD%A6%E5%BF%A0%E5%91%8A/" rel="next" title="大学忠告">
      大学忠告 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#字面量：数字和字符串"><span class="nav-number">1.</span> <span class="nav-text">字面量：数字和字符串</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#总结"><span class="nav-number">1.1.</span> <span class="nav-text">总结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#变量的概念"><span class="nav-number">2.</span> <span class="nav-text">变量的概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#变量的声明和赋值"><span class="nav-number">3.</span> <span class="nav-text">变量的声明和赋值</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#变量的声明-变量的定义"><span class="nav-number">3.1.</span> <span class="nav-text">变量的声明&#x2F;变量的定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#变量的声明-定义（ES6）"><span class="nav-number">3.2.</span> <span class="nav-text">变量的声明&#x2F;定义（ES6）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#变量的赋值"><span class="nav-number">3.3.</span> <span class="nav-text">变量的赋值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#变量的初始化"><span class="nav-number">3.4.</span> <span class="nav-text">变量的初始化</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#变量声明和赋值的补充"><span class="nav-number">4.</span> <span class="nav-text">变量声明和赋值的补充</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#修改变量的值"><span class="nav-number">4.1.</span> <span class="nav-text">修改变量的值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#同时声明多个变量"><span class="nav-number">4.2.</span> <span class="nav-text">同时声明多个变量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#变量声明的几种特殊情况"><span class="nav-number">4.3.</span> <span class="nav-text">变量声明的几种特殊情况</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#变量的命名规范"><span class="nav-number">5.</span> <span class="nav-text">变量的命名规范</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#标识符、关键字、保留字"><span class="nav-number">6.</span> <span class="nav-text">标识符、关键字、保留字</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#标识符"><span class="nav-number">6.1.</span> <span class="nav-text">标识符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#关键字"><span class="nav-number">6.2.</span> <span class="nav-text">关键字</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#保留字"><span class="nav-number">6.3.</span> <span class="nav-text">保留字</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#举例"><span class="nav-number">7.</span> <span class="nav-text">举例</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Gip886"
      src="/images/%E5%A4%B4%E5%83%8F.jpg">
  <p class="site-author-name" itemprop="name">Gip886</p>
  <div class="site-description" itemprop="description">为了更美好的明天而战</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">29</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/Gip886" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;Gip886" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



      </div>
        <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=300 height=86 src="//music.163.com/outchain/player?type=2&id=492390949&auto=1&height=66"></iframe>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Gip886</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">153k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">2:19</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/canvas-ribbon/canvas-ribbon.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>


  




  
<script src="/js/local-search.js"></script>













  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/koharu.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"log":false,"tagMode":false});</script></body>
<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/clicklove.js"></script>
</html>

